package cn.lee.cplibrary.util;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.os.Build;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import java.lang.reflect.Method;

import cn.lee.cplibrary.init.CPInitializer;

/**
 * function:屏幕工具辅助类
 *
 * @author ChrisLee
 * @date 2018/5/25
 */

public class ScreenUtil {

    private ScreenUtil() {
        /* cannot be instantiated */
        throw new UnsupportedOperationException("cannot be instantiated");
    }

    /**
     * 获得屏幕宽度
     */
    public static int getScreenWidth(Context context) {
        CPInitializer.checkInitialization();
        WindowManager wm = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        return outMetrics.widthPixels;
    }

    /**
     * 获得屏幕高度
     */
    public static int getScreenHeight(Context context) {
        CPInitializer.checkInitialization();
        WindowManager wm = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        return outMetrics.heightPixels;
    }



    public static int dp2px(Context context, float dpVal) {
        CPInitializer.checkInitialization();
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpVal * scale + 0.5f);
//        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
//                dpVal, context.getResources().getDisplayMetrics());
    }

    public static int px2dp(Context context, float pxValue) {
        CPInitializer.checkInitialization();
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    public static int px2sp(Context context, float pxValue) {
        CPInitializer.checkInitialization();
        float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (pxValue / fontScale + 0.5f);
    }

    public static int sp2px(Context context, float spValue) {
        CPInitializer.checkInitialization();
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
//        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
//                spValue, context.getResources().getDisplayMetrics());
    }
    public static int sp(Context context,float spValue) {
        CPInitializer.checkInitialization();
        return  (int)spValue;
    }
    /**
     * 获取view以整个页面为参照,距离页面最上端的距离.
     */
    public static int getLocationOnScreenY(View view) {
        CPInitializer.checkInitialization();
        int[] position = new int[2];
        view.getLocationOnScreen(position);
        return position[1];
    }

    /**
     * 获取view以整个页面为参照,距离页面最左端的距离.
     */
    public static int getLocationOnScreenX(View view) {
        CPInitializer.checkInitialization();
        int[] position = new int[2];
        view.getLocationOnScreen(position);
        return position[0];
    }

    /**
     * 获取view以手机屏幕为参照 ,距离屏幕最左端的距离.
     */
    public static int getLocationInWindowX(View view) {
        CPInitializer.checkInitialization();
        int[] position = new int[2];
        view.getLocationInWindow(position);
        return position[0];
    }

    /**
     * 获取view以手机屏幕为参照 ,距离屏幕最上端的距离.
     */
    public static int getLocationInWindowY(View view) {
        CPInitializer.checkInitialization();
        int[] position = new int[2];
        view.getLocationInWindow(position);
        return position[1];
    }

    /**
     * 顺时针旋转
     *
     * @param view：旋转的控件
     * @param rotation:旋转角度
     */
    public static void rotate(Activity activity, View view, float rotation) {
        CPInitializer.checkInitialization();
        DisplayMetrics displayMetrics = new DisplayMetrics();
        // 获取屏幕的尺寸
        activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        // 屏幕宽
        int width = displayMetrics.widthPixels;
        // 屏幕高
        int height = displayMetrics.heightPixels;
        // 设置布局的宽和高,必须要和屏幕的反过来
        if (view.getParent() instanceof LinearLayout) {
            view.setLayoutParams(new LinearLayout.LayoutParams(height, width));
        } else if (view.getParent() instanceof RelativeLayout) {
            view.setLayoutParams(new RelativeLayout.LayoutParams(height, width));
        } else if (view.getParent() instanceof FrameLayout) {
            view.setLayoutParams(new FrameLayout.LayoutParams(height, width));
        } else {
            throw new IllegalArgumentException("view 的父布局必须是LinearLayout、RelativeLayout或FrameLayout其中之一");
        }
        // 顺时针旋转90度
        view.setRotation(rotation);
        // 将布局位移到屏幕中心
        view.setY((height - width) / 2);
        view.setX((width - height) / 2);
    }
    /**
     * 设置Activity竖屏
     */
    public static void setActivityPortrait(Activity activity) {
        CPInitializer.checkInitialization();
        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);// 变为竖屏显示
    }

    /**
     * 设置Activity横屏
     */
    public static void setActivityLandscape(Activity activity) {
        CPInitializer.checkInitialization();
        if (activity.getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {// 无法进行画面的旋转
            return;
        } else {
            if (activity.getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) { // 如果为竖屏显示
                activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); // 变为横屏显示
            }
        }
    }

    public static int getStatusHeight(Context context) {
        CPInitializer.checkInitialization();
        int statusHeight = -1;
        try {
            Class<?> clazz = Class.forName("com.android.internal.R$dimen");
            Object object = clazz.newInstance();
            int height = Integer.parseInt(clazz.getField("status_bar_height").get(object).toString());
            statusHeight = context.getResources().getDimensionPixelSize(height);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        return statusHeight;
    }

    //获取虚拟按键的高度
    public static int getNavigationBarHeight(Context context) {
        CPInitializer.checkInitialization();
        int result = 0;
        if (hasNavBar(context)) {
            Resources res = context.getResources();
            int resourceId = res.getIdentifier("navigation_bar_height", "dimen", "android");
            if (resourceId > 0) {
                result = res.getDimensionPixelSize(resourceId);
            }
        }
        return result;
    }

    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    public static boolean hasNavBar(Context context) {
        CPInitializer.checkInitialization();
        Resources res = context.getResources();
        int resourceId = res.getIdentifier("config_showNavigationBar", "bool", "android");
        if (resourceId != 0) {
            boolean hasNav = res.getBoolean(resourceId);
            // check override flag
            String sNavBarOverride = getNavBarOverride();
            if ("1".equals(sNavBarOverride)) {
                hasNav = false;
            } else if ("0".equals(sNavBarOverride)) {
                hasNav = true;
            }
            return hasNav;
        } else { // fallback
            return !ViewConfiguration.get(context).hasPermanentMenuKey();
        }
    }

    private static String getNavBarOverride() {
        String sNavBarOverride = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            try {
                Class c = Class.forName("android.os.SystemProperties");
                Method m = c.getDeclaredMethod("get", String.class);
                m.setAccessible(true);
                sNavBarOverride = (String) m.invoke(null, "qemu.hw.mainkeys");
            } catch (Throwable e) {
            }
        }
        return sNavBarOverride;
    }

}
